﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Modellayer;
using Dblayer;

namespace ControlLayer
{
    public class ShowRoomCtr
    {
        DbShowRoom dbShowRoom;
        DbShow dbShow;
        private static ShowRoomCtr instance;

        private ShowRoomCtr()
        {
            dbShowRoom = new DbShowRoom();
            dbShow = new DbShow();
        }

        public static ShowRoomCtr getInstance()
        {

            if (instance == null)
            {
                instance = new ShowRoomCtr();
            }
            return instance;
        }

        public void createShowRoom(int showRoomNumber, int numberOfSeats)
        {
            ShowRoom showRoom = new ShowRoom(showRoomNumber, numberOfSeats);
            dbShowRoom.createShowRoom(showRoom);
        }

        public void updateNumberOfSeats(int showRoomNumber, int numberOfSeats)
        {
            ShowRoom showRoom = dbShowRoom.findShowroomWithoutDescription(showRoomNumber);
            showRoom.numberOfSeats = numberOfSeats;
            dbShowRoom.updateShowRoom(showRoom);
        }

        public void addDescription(int showRoomNumber, string showRoomDescription)
        {
            ShowRoom showRoom = dbShowRoom.findShowroom(showRoomNumber);
            showRoom.showRoomDescription = showRoomDescription;
            dbShowRoom.updateShowRoom(showRoom);
        }

        public ShowRoom findShowRoom(int showRoomNumber)
        {
            return dbShowRoom.findShowroom(showRoomNumber);
        }

        public List<ShowRoom> findAllShowRooms()
        {
            return dbShowRoom.findAllShowRooms();
        }

        public List<ShowRoom> findFreeShowRoomsByDate(string date)
        {
            List<ShowRoom> showRooms = dbShowRoom.findAllShowRooms();
            List<Show> shows = dbShow.findShowsByDate(date);
            foreach (Show show in shows)
            {
                ShowRoom showRoom = CompareShowRoom(showRooms, show.showroom.showRoomNumber);
                if (showRoom != null)
                {

                    showRooms.Remove(showRoom);
                }

            }
            return showRooms;
        }

        private ShowRoom CompareShowRoom(List<ShowRoom> showRooms, int showRoomNumber)
        {
            int index = 0;
            Boolean found = false;
            while (index < showRooms.Count() && !found)
            {
                ShowRoom showroom = showRooms.ElementAt(index);
                if (showroom.showRoomNumber == showRoomNumber)
                {
                    found = true;
                }
                else
                {
                    index++;
                }

            }
            if (found)
            {
                return showRooms.ElementAt(index);
            }
            else
            {
                return null;
            }

        }

    }
}
